﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;

namespace BookSystem.Admin
{
    public partial class AdminGetUser : Form
    {
        public AdminGetUser()
        {
            InitializeComponent();
        }

        SqlConnection conn = SQLHelper.getConnnection();
        SqlCommand cmd;

        string sql = "select * from T_user";

        //窗口载入初始化
        private void AdminGetUser_Load(object sender, EventArgs e)
        {
            dataBinding(sql);
            dataGridView1.ReadOnly = true;
        }

        //查询按钮
        private void get_Click(object sender, EventArgs e)
        {
            if (uid.Text == string.Empty)
            {

                MessageBox.Show("请输入关键字图书编号", "提示");
                uid.Focus();
                uid.SelectAll();
                return;

            }


            string sSQL = "select * from T_user where Uid = " + uid.Text;

            //刷新DataGridView控件
            dataBinding(sSQL);
        }

        //增加按钮
        private void add_Click(object sender, EventArgs e)
        {
            if (uid.Text != string.Empty
                && uName.Text != string.Empty
                && pwd.Text != string.Empty && !BLL.User.isEquals(uName.Text))
            {
                String strSql = "insert into T_user values ('" + uid.Text + "','" +
                    uName.Text + "','" + pwd.Text + "')";

                cmd = new SqlCommand(strSql, conn);

                try
                {
                    cmd.ExecuteNonQuery();

                }
                catch (SqlException sqlEx)
                {

                    MessageBox.Show("错误：可能是相同用户编号引起的！", sqlEx.ToString());

                }


                //刷新DataGridView控件
                dataBinding(sql);
            }
            else
            {

                MessageBox.Show("输入不能为空，并且名字不能重复！");

            }
        }

        //更新按钮
        private void update_Click(object sender, EventArgs e)
        {
            if (uid.Text != string.Empty
                && uName.Text != string.Empty
                && pwd.Text != string.Empty)
            {
                model.User u = new model.User(uid.Text, uName.Text, pwd.Text);
                BLL.User.updateUser(u);
            }


            //刷新DataGridView控件
            dataBinding(sql);
        }

        //删除按钮
        private void delete_Click(object sender, EventArgs e)
        {
            if (uid.Text != string.Empty)
            {
                String strSql = "delete from T_user where Uid = '" + uid.Text + "'";

                cmd = new SqlCommand(strSql, conn);

                cmd.ExecuteNonQuery();
            }

            //刷新DataGridView控件
            dataBinding(sql);
        }


        //绑定dataGridView
        private void dataBinding(String strSql)
        {

            SqlDataAdapter da;
            DataSet ds;


            //连接配置


            cmd = new SqlCommand(strSql, conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "myTable");

            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "myTable";
            da.Dispose();



        }

        //dataGridView单击事件
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            uid.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            uName.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            pwd.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
        }

        //窗口关闭事件
        private void AdminGetUser_FormClosed(object sender, FormClosedEventArgs e)
        {
            cmd.Dispose();
            conn.Close();
        }



    }
}